﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designer.Event.UI;
using CPPEI.Coolzon.Designer.UI;
using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for HeaderView.xaml
    /// </summary>
    public partial class HeaderView : View
    {
        Rect rcnormal;//定义一个全局rect记录还原状态下窗口的位置和大小。
        bool IsWindowStyleMax = true;
        public HeaderView()
        {
            InitializeComponent();

            InitEditMenu();
            if (Application.Current.MainWindow != null)
            {
                Application.Current.MainWindow.StateChanged += MainWindow_StateChanged;
            }
        }

        private void MainWindow_StateChanged(object sender, EventArgs e)
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
            {
                _ctlMax.Style = Resources["btnRestoreStyle"] as Style;
            }
            else
            {
                _ctlMax.Style = Resources["btnMaxStyle"] as Style;
            }
        }

        public void InitEditMenu()
        {
            Type customCommand = typeof(UIPartCommand);
            foreach (var property in customCommand.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static))
            {
                var command = (ICommand)property.GetValue(null);
                MenuItem mi = new MenuItem();
                mi.Command = command;
                _ctlEditMI.Items.Add(mi);
            }
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Application.Current.MainWindow.DragMove();
        }

        private void _ctlMin_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

        private void _ctlMax_Click(object sender, RoutedEventArgs e)
        {
            ChangeWindowState();
        }

        private void ChangeWindowState()
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
            {
                Application.Current.MainWindow.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                Application.Current.MainWindow.WindowState = System.Windows.WindowState.Maximized;
            }
        }

        private void _ctlClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.Close();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                ChangeWindowState();
            }
        }

        /// <summary>
        /// 各窗口关闭事件
        /// </summary>
        /// <param name="@event"></param>
        [Subscribe]
        public void ViewAnchorableVisibleChangeEvent(UIAnchorableVisibleChangeEvent @event)
        {
            switch (@event.ViewAnchorableContentId)
            {
                case "SolutionAnchorable": _ctlSolutionMI.IsChecked = @event.IsVisible; break;
                case "ToolboxAnchorable": _ctlToolboxMI.IsChecked = @event.IsVisible; break;
                case "PropertyAnchorable": _ctlPropertyMI.IsChecked = @event.IsVisible; break;
                case "EventAnchorable": _ctlEventMI.IsChecked = @event.IsVisible; break;
                case "PartTreeAnchorable": _ctlPartTreeMI.IsChecked = @event.IsVisible; break;
            }
        }
    }
}
